//
//  SYBuKeInviteViewController.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2020/3/27.
//  Copyright © 2020年 SanYi. All rights reserved.
//

#import "SYBuKeInviteViewController.h"
#import "SYTopLabView.h"
#import "SYInviteTableViewCell.h"
#import "SYClassHourView.h"
#import "SYSelectClassViewController.h"


@interface SYBuKeInviteViewController () <UITableViewDelegate,UITableViewDataSource,SYSelectClassViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *inviteBtn;//补课邀请
@property (weak, nonatomic) IBOutlet UIButton *selectClassBtn;
@property (weak, nonatomic) IBOutlet UITableView *curTableView;
@property (strong, nonatomic) SYTopLabView *topLabView;
@property (strong, nonatomic) NSMutableArray *dataArr;
@property (strong, nonatomic) SYClassHourView *hourView;
@property (strong, nonatomic) SYSelectClassViewController *selectClassVC;
@property (copy, nonatomic) NSString *classId;

@end

@implementation SYBuKeInviteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
   
    //班级全部默认是-1
    self.classId = @"-1";
    
    [_curTableView registerCellWithNIbNames:@[@"SYInviteTableViewCell"]];
    __weak typeof(self) weakSelf = self;
    [_curTableView headerRefreshBlock:^{
//        NSInteger page = weakSelf.dataArr.count/10 <= 1?1:weakSelf.dataArr.count/10 +1;
        [self requestDataListPage:1];
        
    }];
    
    [_curTableView fooderRefreshBlock:^{
        NSInteger page = weakSelf.dataArr.count/10 <= 1?1:weakSelf.dataArr.count/10 +1;
        [self requestDataListPage:page];
    }];
    
    
    [_curTableView.mj_header beginRefreshing];
    
    
//    __weak typeof(self) weakSelf = self;
//    _selectClassVC.selectBlock = ^(NSString * _Nonnull classId, id  _Nonnull className) {
//        weakSelf.classId = classId;
//        [weakSelf requestDataListPage:1];
//        [weakSelf.selectClassVC dismissViewControllerAnimated:YES completion:nil];
//
//    };
    
}

- (void)selectClassViewClassId:(NSString *)classId className:(nonnull NSString *)className{
    self.classId = classId;
    [self.selectClassBtn setTitle:className forState:UIControlStateNormal];
    [self requestDataListPage:1];
    [self.selectClassVC dismissViewControllerAnimated:YES completion:nil];
}


- (void)requestDataListPage:(NSInteger)page {
    
    __weak typeof(self) weakSelf = self;
    [SYRequestAPI fd_requestClassLessonInvitationListClassId:self.classId pageIndex:SJStringWithFormat(page) pageSize:@"10" success:^(NSMutableArray *listArr) {
        [weakSelf.curTableView sy_endRefreshing];
        [weakSelf.dataArr removeAllObjects];
        
        [weakSelf.dataArr addObjectsFromArray:listArr];
        [weakSelf.curTableView reloadData];
    } fail:^(NSError *error) {
        [weakSelf.curTableView sy_endRefreshing];
        [MBProgressHUD zr_showError:error.domain];
    }];
    
    
}


#pragma mark - tab
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.0001;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] init];
    _topLabView = [[SYTopLabView alloc] initWithDataNameArr:@[@"课程",@"课时",@"上课时间",@"学生",@"学号",@"邀请时间",@"是否确认",@"确认补课时间",@"发放"] width:0.11111];
    [headerView addSubview:_topLabView];
    [_topLabView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.equalTo(headerView);
    }];
    return headerView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SYInviteTableViewCell *inviteCell = [tableView dequeueReusableCellWithIdentifier:@"SYInviteTableViewCell" forIndexPath:indexPath];
    inviteCell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *dict = self.dataArr[indexPath.row];
    inviteCell.kechengLab.text = [dict nilObjectForKey:@"courseName"];
    inviteCell.keshiLab.text = [dict nilObjectForKey:@"lesson"];
    inviteCell.shangkeLab.text = [dict nilObjectForKey:@"beginDate"];
    inviteCell.xuehaoLab.text = [dict nilObjectForKey:@"serial"];
    inviteCell.xueshengLab.text = [dict nilObjectForKey:@"studentName"];
    inviteCell.yaoqingTimeLab.text = [dict nilObjectForKey:@"inviteDate"];
    NSString *isConfirm = [dict nilObjectForKey:@"isConfirm"];
    if ([isConfirm isEqualToString:@"0"]) {
        inviteCell.querenLab.text = @"未确认";
    } else {
        inviteCell.querenLab.text = @"已确认";
    }
    inviteCell.querenTimeLab.text = [dict nilObjectForKey:@"confirmDate"];
    inviteCell.fafangLab.text = @"发放视频";
    
    return inviteCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 
    NSDictionary *dict = self.dataArr[indexPath.row];
    [MBProgressHUD mj_showLoading];
    [SYRequestAPI fd_requestClassGrantVideoStudentId:[dict nilObjectForKey:@"studentId"] scheduleId:[dict nilObjectForKey:@"scheduleId"] success:^(BOOL isOk) {
        [MBProgressHUD mj_hideHUD];
        [MBProgressHUD zr_showError:@"发送成功"];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUD];
        [MBProgressHUD zr_showError:error.domain];
    }];
    
    
    
}


#pragma mark - 初始化
- (void)initSubView {
    _inviteBtn.layer.cornerRadius = 10;
    _inviteBtn.layer.masksToBounds = YES;
    _inviteBtn.layer.borderColor = [UIColor colorWithHexString:@"#20B097"].CGColor;
    _inviteBtn.layer.borderWidth = 1;
    
    _selectClassBtn.layer.borderColor = [UIColor colorWithHexString:@"#333333"].CGColor;
    _selectClassBtn.layer.borderWidth = 1;
    
    
    _curTableView.delegate = self;
    _curTableView.dataSource = self;
    
    _dataArr = [NSMutableArray array];
}

#pragma mark - btn
//点击补课邀请
- (IBAction)makeInviteBtnClick:(UIButton *)sender {
    
    _hourView = [[SYClassHourView alloc] initWithFrame:CGRectMake(kScreen_WIDTH, 0, CGRectGetWidth(self.view.frame), kScreen_HEIGHT-20)];
    [self.view addSubview:_hourView];
    [_hourView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.equalTo(self.view);
    }];
//    __weak typeof(self) weakSelf = self;
    [NSObject executeRunloop:^{
        //        请求数据
//        [self.hourView requestDataListClassId:@""];
        self.hourView.contVC = self;
    } afterDelay:kAnimate];
//    [UIView animateWithDuration:kAnimate animations:^{
//        weakSelf.hourView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), kScreen_HEIGHT-20);
//    }];
}
- (IBAction)selectClassBtnClick:(UIButton *)sender {
    _selectClassVC = [[SYSelectClassViewController alloc] init];
    _selectClassVC.delegate = self;
    //  弹出视图的显示样式
    self.selectClassVC.modalPresentationStyle = UIModalPresentationPopover;
    
    //  1、弹出视图的大小
    self.selectClassVC.preferredContentSize = CGSizeMake(350, 400);
    self.selectClassVC.popoverPresentationController.sourceView = sender;
    self.selectClassVC.popoverPresentationController.sourceRect = sender.bounds;
    //箭头得方向
    self.selectClassVC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;
    self.selectClassVC.popoverPresentationController.backgroundColor = [UIColor whiteColor];
    
    __weak typeof(self) weakSelf = self;
    [self presentViewController:self.selectClassVC animated:YES completion:^{
        [weakSelf.selectClassVC requestDataList];
    }];
}

- (SYSelectClassViewController *)selectClassVC {
    if (!_selectClassVC) {
        _selectClassVC = [[SYSelectClassViewController alloc] init];
    }
    return _selectClassVC;
}


#pragma mark -
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
